package com.qj.yantaishan.service.impl;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.IService;
import com.qj.yantaishan.service.IProductVoService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import com.qj.yantaishan.mapper.ProductCategoryMapper;
import com.qj.yantaishan.domain.ProductCategory;
import com.qj.yantaishan.service.IProductCategoryService;
import com.qj.yantaishan.common.core.text.Convert;

/**
 * 商品分类Service业务层处理
 *
 * @author qihe
 * @date 2022-07-22
 */
@Service
@Primary
@Slf4j
public class ProductCategoryServiceImpl extends ServiceImpl<ProductCategoryMapper, ProductCategory> implements IProductCategoryService {
    @Autowired
    private ProductCategoryMapper productCategoryMapper;
    @Autowired
    private IProductVoService productVoService;

    /**
     * 查询商品分类
     *
     * @param catId 商品分类主键
     * @return 商品分类
     */
    @Override
    public ProductCategory selectProductCategoryByCatId(Integer catId) {
        return productCategoryMapper.selectProductCategoryByCatId(catId);
    }

    /**
     * 查询商品分类列表
     *
     * @param productCategory 商品分类
     * @return 商品分类
     */
    @Override
    public List<ProductCategory> selectProductCategoryList(ProductCategory productCategory) {
        return productCategoryMapper.selectProductCategoryList(productCategory);
    }

    /**
     * 新增商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int insertProductCategory(ProductCategory productCategory) {
        return productCategoryMapper.insertProductCategory(productCategory);
    }

    /**
     * 修改商品分类
     *
     * @param productCategory 商品分类
     * @return 结果
     */
    @Override
    public int updateProductCategory(ProductCategory productCategory) {
        return productCategoryMapper.updateProductCategory(productCategory);
    }

    /**
     * 批量删除商品分类
     *
     * @param catIds 需要删除的商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByCatIds(String catIds) {
        return productCategoryMapper.deleteProductCategoryByCatIds(Convert.toStrArray(catIds));
    }

    /**
     * 删除商品分类信息
     *
     * @param catId 商品分类主键
     * @return 结果
     */
    @Override
    public int deleteProductCategoryByCatId(Integer catId) {
        return productCategoryMapper.deleteProductCategoryByCatId(catId);
    }

    @Override
    public List<ProductCategory> myList() {
        /**
         * 获取分类列表并获取分类的子分类
         * 思路:
         *   先获取所有分类-->查看cat_fid(父分类id）0为一级分类 |不为0则为子分类
         *      不为零进入递归在所有分类中找他爹
         *  ...-->三级-->二级-->一级  cat_fid = cat_id
         *  一级-->二级-->三级....  cat_id = cat_fid
         */
        //所有分类
        List<ProductCategory> categoryList = list(new LambdaQueryWrapper<ProductCategory>().eq(ProductCategory::getAgentId, "8581960447"));
        //父分类
        List<ProductCategory> fatherCategory = categoryList.stream().filter(category -> {
            return category.getCatFid() == 0;
        }).collect(Collectors.toList());
        //遍历每个父分类在所有分类中找他的子分类
        List<ProductCategory> productCategoryList = fatherCategory.stream().map(fCategory -> {
            fCategory.setChildCategory(getChildCategory(fCategory, categoryList));
            //查找对应的商品数

            int fCount = productVoService.getProductCountByFatherCategoryId(fCategory.getCatId());
            fCategory.setProductCount(fCount);
            return fCategory;
        }).collect(Collectors.toList());

        return productCategoryList;
    }

    private List<ProductCategory> getChildCategory(ProductCategory fCategory, List<ProductCategory> categoryList) {
        List<ProductCategory> productCategories = categoryList.stream().filter(c -> {
            return fCategory.getCatId().equals(c.getCatFid());
        }).collect(Collectors.toList());
        return productCategories.stream().map(f -> {
            f.setChildCategory(getChildCategory(f, categoryList));
            f.setProductCount(productVoService.getProductCountByCategoryId(f.getCatId()));
            return f;
        }).collect(Collectors.toList());
    }

    private List<ProductCategory> getCategory(List<ProductCategory> categoryList, List<ProductCategory> fatherCategory) {
        List<ProductCategory> collect = fatherCategory.stream().map(fCategory -> {
            //遍历所有分类
            List<ProductCategory> childCategoryList = categoryList.stream().filter(allCategory -> {
                log.info("catId" + fCategory.getCatId().toString());
                log.info("catFid" + allCategory.getCatFid().toString());
                return fCategory.getCatId().equals(allCategory.getCatFid());
            }).collect(Collectors.toList());
            fCategory.setChildCategory(childCategoryList);

            return fCategory;
        }).collect(Collectors.toList());

        return collect;
    }
}
